<?php
/**
 * Aero - Xistins Technologies PHP Framework
 * 
 * LICENSE
 * This source file is subject to the New BSD license that is bundled 
 * with this package in the LICENSE.txt. It is also available through 
 * the world-wide-web at this URL: http://dev.xistins.com/license/production
 * If you did not receive a copy of this license and are unable to obtain
 * it through the world-wide-web, please send an email to 
 * license@xistins.com so we can send you a copy immediately.
 * 
 * @category   Aero
 * @package    Aero_Db
 * @subpackage Adapter
 * @copyright  http://dev.xistins.com/license/production     New BSD
 * @version    $Id: Mysql.php 4 2009-11-14 18:35:45Z csouth@xistins.com $
 */

/** @see Aero_Db_Adapter */
require_once 'Aero/Db/Adapter.php';

/**
 * MySQL adapter
 * 
 * @category   Aero
 * @package    Aero_Db
 * @subpackage Adapter
 * @copyright  http://dev.xistins.com/license/production     New BSD
 * @version    $Id: Mysql.php 4 2009-11-14 18:35:45Z csouth@xistins.com $
 */
class Aero_Db_Adapter_Mysql extends Aero_Db_Adapter
{
    /**
     * Close database connection
     * 
     * @return void
     */
    public function closeConnection()
    {
        mysql_close($this->_connection);
        $this->_connection = null;
    }

    /**
     * Connect to the database
     * 
     * @return void
     */
    protected function _connect()
    {
        @$this->_connection = mysql_connect($this->_config->host, $this->_config->username, $this->_config->password);

        if(!$this->_connection || !mysql_select_db($this->_config->dbname, $this->_connection)) {
            Aero_Loader::loadClass('Aero_Db_Exception');
            throw new Aero_Db_Exception('MySQL connection error :' . mysql_error());
        }
    }

    /**
     * Describe Table
     * 
     * @param  string $tableName
     * @return array
     */
    public function describeTable($tableName)
    {
        $desc = $this->query("DESCRIBE $tableName;");
        foreach($desc as $descTemp) {
            $colName = $descTemp['Field'];
            unset($descTemp['Field']);
            foreach($descTemp as $key => $value) {
                if($value == '') {
                    unset($descTemp[$key]);
                }
            }
            $retVal[$colName] = $descTemp;
        }
        return $retVal;
    }

    /**
     * Escape a variable for query usage
     * 
     * @param  string|int|float $var
     * @return string
     */
    public function escape($var)
    {
        if(!is_string($var) && !is_int($var) && !is_float($var)) {
            Aero_Loader::loadClass('Aero_Db_Exception');
            throw new Aero_Db_Exception('Variable needing escaped must be a string, integer, or float.');
        }

        $this->getConnection();
        return mysql_real_escape_string($var);
    }

    /**
     * Execute a resultless query
     * 
     * @param  string $sql
     * @return bool
     */
    public function exec($sql)
    {
        $this->getConnection();
        return (bool)$this->query($sql);
    }

    /**
     * List all tables in the database
     * 
     * @param  bool $descTables Describe tables
     * @return mixed
     */
    public function listTables($descTables = false)
    {
        $tableResult = $this->query('SHOW TABLE STATUS;');
        foreach($tableResult as $table) {
            $tableName = $table['Name'];
            unset($table['Name']);
            if($descTables) {
                $desc = $this->describeTable($tableName);
                $table['columnInfo'] = $desc;
            }
            $tables[$tableName] = $table;
        }
        return $tables;
    }

    /**
     * Run query on the database
     * 
     * @return array|bool
     */
    public function query($sql)
    {
        $this->getConnection();
        $results = @mysql_query($sql, $this->_connection);

        if(!$results) {
            return false;
        }

        while($row = mysql_fetch_assoc($results)) {
            $return[] = $row;
        }
        mysql_free_result($results);
        return $return;
    }
}